<?php

/**
 * uniBoard ACP Help Files page
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-15 17:35:38 +0000 (Wed, 15 Sep 2010) $
 * $Revision: 61 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_help_system{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Load fields templates
		$appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
		
		// Cache output
		ob_start();
		
		// Open table block
		echo $appCore -> output -> openTableBlock();
		
		// Draw header
		echo $appCore -> output -> tableHeader( array(
			array(
				'left_float width_quarter',
				$appCore -> lang['acp_help_files_list_name']
			),
			array(
				'right_float width_medium',
				$appCore -> lang['actions']
			),
			array(
				'right_float width_medium',
				$appCore -> lang['column_position']
			)
		));
		
		// Reorder items?
		if ( $appCore -> _POST( 'do') == 'reorder')
		{
			// Grab new positions
			$new_positions = $appCore -> _POST( 'file_pos');
			
			// New positions are not empty array?
			if ( !empty( $new_positions) && is_array( $new_positions))
			{
				// Reiterate
				foreach ( $new_positions as $file_id => $new_pos)
				{
					// New pos is not array?
					if ( !is_array( $new_pos))
					{
						// Force integers
						settype( $file_id, 'integer');
						settype( $new_pos, 'integer');
						
						// File ID greater than ZERO?
						if ( $file_id > 0)
						{
							$appCore -> db -> update( array( 'file_pos' => $new_pos), 'help_files', 'file_id = \'' . $file_id . '\'');
						}
					}
				}
				
				// Kill cache
				class_cache::flushCache( 'help_files');
				
				// Set Log
				$appCore -> logAdmin( 'log_admin_reordered_help_files');
								
				// Draw message
				$appCore -> output -> message( $appCore -> lang['acp_help_files_reordered'], $appCore -> lang['acp_help_system']);
			}
		}
		
		// Unordered Files List
		$files_list = array();
		
		// Select Files
		$appCore -> db -> query( 'SELECT file_id, file_parent, file_pos, file_name
		FROM ' . DB_PREFIX . 'help_files
		ORDER BY file_parent, file_pos ASC');
		
		// Parse result
		while ( $file_result = $appCore -> db -> fetch_assoc())
		{
			// Create empty parent array
			if ( !isset( $files_list[$file_result['file_parent']]))
			{
				$files_list[$file_result['file_parent']] = array();
			}
			
			// Add File to array
			$files_list[$file_result['file_parent']][$file_result['file_id']] = $file_result;
		}
		
		// Free result
		$appCore -> db -> freeResult();
		
		// Order Files
		$files_list = class_hierarchy::doOrdering( $files_list, array(
			$appCore -> output -> drawImage( 'tree_s'),
			$appCore -> output -> drawImage( 'tree_i'),
			$appCore -> output -> drawImage( 'tree_t'),
			$appCore -> output -> drawImage( 'tree_l')
		));
		
		// Found files?
		if (!empty( $files_list))
		{
			// Open form
			echo $appCore -> output -> openForm( $appCore -> adminLink( 'act=help_system'));	
			echo $appCore -> drawHidden( 'do', 'reorder');	
		}
		
		// Reiterate Files
		foreach ( $files_list as $file_data)
		{
			// Draw row
			echo $appCore -> output -> tableRow('
				<div class="cell left_float width_longer">
					' . $file_data['item_image'] . '<b> ' . $appCore -> ifString( htmlspecialchars( $file_data['file_name'])) . '</b>
				</div>
				<div class="cell left_border right_float width_medium center">
					<a href="' . $appCore -> adminLink( 'act=edit_help&do=edit&file=' . $file_data['file_id']) . '">' . $appCore -> output -> drawImage( 'action_edit', $appCore -> lang['edit']) . '</a>
					<a href="' . $appCore -> adminLink( 'act=edit_help&do=delete&file=' . $file_data['file_id']) . '">' . $appCore -> output -> drawImage( 'action_delete', $appCore -> lang['delete']) . '</a>
				</div>
				<div class="cell left_border right_float width_medium center">
					' . $appCore -> drawTextInput( 'file_pos[' . $file_data['file_id'] . ']', $file_data['file_pos'], '90px') . '
				</div>');
		}
		
		// Found files?
		if (!empty( $files_list))
		{
			// Close Form
			echo $appCore -> output -> submitButton( $appCore -> lang['reorder_items']);
			echo $appCore -> output -> closeForm();	
		}
		
		// Close table block
		echo $appCore -> output -> closeTableBlock();
		
		// New Language File Form
		echo $appCore -> output -> openForm( $appCore -> adminLink( 'act=new_help', true));
		echo $appCore -> output -> openFormBlock( $appCore -> lang['new_help_file_form']);
		echo $appCore -> output -> submitButton( $appCore -> lang['new_help_file_form_submit']);
		echo $appCore -> output -> closeFormBlock();
		echo $appCore -> output -> closeForm();
		
		// Draw output
		$appCore -> output -> draw( $appCore -> lang['acp_help_system'], array( $appCore -> adminLink( 'act=help_system') => $appCore -> lang['acp_help_system']), ob_get_clean());
				
	}

}